package com.hitqz.robot.biz.flow.component.patrol;

import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.hitqz.robot.api.business.entity.PatrolResultEntity;
import com.hitqz.robot.api.business.model.dto.FlowContextDto;
import com.hitqz.robot.api.business.model.dto.FlowPatrolPreAndEndTicketsDto;
import com.hitqz.robot.api.common.entity.*;
import com.hitqz.robot.biz.service.*;
import com.yomahub.liteflow.annotation.LiteflowComponent;
import com.yomahub.liteflow.core.NodeComponent;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Slf4j
@LiteflowComponent(value = "GenerateResults", name = "[巡检]生成检测报告模板")
@AllArgsConstructor
public class GenerateResults extends NodeComponent {

    private final PatrolResultService patrolResultService;

    private final PatrolItemService patrolItemService;

    private final PatrolObjectService patrolObjectService;

    private final PatrolPosPoseGroupService patrolPosPoseGroupService;

    private final PatrolDevService patrolDevService;

    @Override
    public void process() throws Exception {
        FlowContextDto dto = this.getRequestData();
        TicketsTask task = dto.getTicketsTask();
        log.info("任务数据 {}", task.getParseOrderParams());
        FlowPatrolPreAndEndTicketsDto params = JSONObject.parseObject(task.getParseOrderParams(), FlowPatrolPreAndEndTicketsDto.class);
        log.info("{} ---params: {}", task.getParseOrderParams(), JSONObject.toJSONString(params));
        if (StrUtil.isBlank(params.getPatrolItemIds())){
            log.warn("没有检测项信息,不执行");
            return;
        }
        List<PatrolResultEntity> list = patrolResultService.list(Wrappers.<PatrolResultEntity>lambdaQuery()
                .eq(PatrolResultEntity::getTicketsId, dto.getTickets().getId()));
        if (!list.isEmpty()){
            log.info("已有检测结果模版,不再创建");
            return;
        }
        List<PatrolResultEntity> resultList = new ArrayList<>();
        String[] items = params.getPatrolItemIds().split(",");
        for (String itemId : items) {
            if (StrUtil.isBlank(itemId)){
                continue;
            }
            PatrolResultEntity patrolResult = new PatrolResultEntity();
            patrolResult.setTicketsId(dto.getTickets().getId().intValue());
            PatrolItem item = patrolItemService.getById(itemId);
            patrolResult.setItemName(item.getName());
            patrolResult.setItemId(item.getId().intValue());
            PatrolObject patrolObject = patrolObjectService.getById(item.getPatrolObjectId());
            patrolResult.setPatrolObjectId(patrolObject.getId().intValue());
            patrolResult.setCreateTime(new Date());
            patrolResult.setStatus(0);
            patrolResult.setUploadStatus(0);
            PatrolPosPoseGroup posPoseGroup = patrolPosPoseGroupService.getById(item.getPatrolPosPoseGroupId());
            PatrolDev dev = patrolDevService.getById(posPoseGroup.getDevId());
            patrolResult.setDevName(dev.getName());
            PatrolDev room = patrolDevService.getById(dev.getParentId());
            patrolResult.setRoomName(room.getName());
            resultList.add(patrolResult);
        }
        if (!resultList.isEmpty()){
            patrolResultService.saveBatch(resultList);
        }

    }
}